﻿<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Chat.aspx.cs" Inherits="ChatApp._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript">
    function blockSubmit() {
        send_msg();
        return false;
    }
    function send_msg() {
        document.getElementById("chatInfo").innerHTML += '<br>' + 'ИЗПРАЩАНЕ : ' + document.getElementById("textToSend").value;
        var xmlhttp = false;
        try {
            xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); // For Firefox
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); // For IE
            } catch (E) {
                xmlhttp = false;
            }
        }
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
            xmlhttp = new XMLHttpRequest();
        }
        var url = 'SendMessage.aspx?text=' + getElementById("textToSend").value + '&sender='; // + getElementById("username").value + '&';
        document.getElementById("textToSend").value = "";
        xmlhttp.open('POST', url, true);
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete") {
                document.getElementById("textToSend").value = "";
            }
        }
        xmlhttp.send(null)
        return;
    }
    function refresh_chat() {
        var xmlhttp = false;
        try {
            xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); // For Firefox
        } catch (e) {
            try {
                xmlhttp = new 
		ActiveXObject('Microsoft.XMLHTTP'); // For IE
            } catch (E) {
                xmlhttp = false;
            }
        }
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
            xmlhttp = new XMLHttpRequest();
        }
        var channelName = document.getElementById("channelNameLabel").innerHTML;
        var url = 'GetMessagesFromChannel.aspx?name=' + channelName;
        xmlhttp.open('POST', url, true);
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 || xmlhttp.readyState == "complete") {
                document.getElementById("chatContent").innerHTML = xmlhttp.responseText;
                document.getElementById("chatInfo").innerHTML = '';
            }
        }
        xmlhttp.send(null)
        setTimeout('refresh_chat();', 2000);
        return;
    }
</script>
</asp:Content>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ListBox ID="channels" runat="server">
    </asp:ListBox>
    <div id="channelNameLabel">Pesho</div>
    <div id="chatContent">
    </div>
    <form name="chatform" method="post" action="" onsubmit="return blockSubmit();">
        <input type="text" name="textToSend" id="textToSend" style="width:300px" />
        <input type="button" value="Send" onclick="blockSubmit()" />
    </form>
    <div id="chatInfo">
    </div>
    <script type="text/javascript">
        refresh_chat();
    </script>
</asp:Content>

